package com.itWaiLong.Service.Impl;

import com.itWaiLong.AppException.AppException;
import com.itWaiLong.AppException.GlobalExceptionHandler;
import com.itWaiLong.Dao.Entities.DeleteProject;
import com.itWaiLong.Dao.personal_project;
import com.itWaiLong.Mapper.PersonalProjectMapper;
import com.itWaiLong.Service.PersonalProjectService;
import com.itWaiLong.Utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PersonalProjectServiceImpl implements PersonalProjectService {

    @Autowired
    private PersonalProjectMapper personalProjectMapper;

    //获取用户id
    public int getUserId(){
        Map<String, Object> map = ThreadLocalUtil.getThreadLocal();
        return (int)map.get("userId");
    }


    public void AddProject(personal_project pProject) {
        log.info("项目信息：{}",pProject);

        //获取用户的id
//        Map<String,Object> map = ThreadLocalUtil.getThreadLocal();
        int userId = getUserId();

        //查看用户是否已经添加过该项目
        personal_project personalProject = personalProjectMapper.findProjectByName(pProject.getProjectName(),userId);
        if (personalProject != null) {
            throw new GlobalExceptionHandler(AppException.XIANG_MU_YI_CUN_ZAI);
        }

        //没有该项目
        pProject.setUserId(userId);
        personalProjectMapper.AddProject(pProject);
    }

    @Override
    public void deleteProject(DeleteProject pProject)  {

        //通过项目名称和用户id查找项目是否存在
        int userId = getUserId();
        pProject.setUserId(userId);

        //判断该项目是否存在
        personal_project projectByName = personalProjectMapper.findProjectByName(pProject.getProjectName(), userId);
        if (projectByName == null) {
            //项目不存在
            throw new GlobalExceptionHandler(AppException.MEI_YOU_GAI_XIANG_MU);
        }
        //项目存在
        personalProjectMapper.deleteProject(pProject);
    }

    /**
     * 编辑项目
     * @param pProject
     * @return
     */
    public void updateProject(personal_project pProject) {

        //获取用户id
        int userId = getUserId();
        pProject.setUserId(userId);

        //判断项目是否存在
        personal_project projectByName = personalProjectMapper.findProjectByName(pProject.getProjectName(), userId);
        if (projectByName == null) {
            //项目不存在
            throw new GlobalExceptionHandler(AppException.MEI_YOU_GAI_XIANG_MU);
        }
        //项目存在
        pProject.setProjectId(projectByName.getProjectId());
        personalProjectMapper.updateProject(pProject);
    }

    @Override
    public List<personal_project> selectAllProject() {

        //获取用户id
        int userId = getUserId();

        List<personal_project> list = personalProjectMapper.selectAllProject(userId);
        if (list.size() == 0 || list == null) {
            throw new GlobalExceptionHandler(AppException.MEI_YOU_SHU_JU);
        }
        return list;
    }

    /**
     * 通过项目名称查找项目
     * @param name
     * @return
     */
    public personal_project selectProjectByName(String name) {

        //获取用户id
        int userId = getUserId();

        //判断项目书否存在
        personal_project projectByName = personalProjectMapper.findProjectByName(name, userId);
        if (projectByName == null) {
            throw new GlobalExceptionHandler(AppException.MEI_YOU_GAI_XIANG_MU);
        }
        return projectByName;
    }

    /**
     * 通过项目id查找用户项目
     * @param projectId
     * @return
     */
    public personal_project selectProjectById(int projectId) {

        personal_project projectById = personalProjectMapper.findProjectById(projectId);
        if (projectById == null) {
            throw new GlobalExceptionHandler(AppException.MEI_YOU_GAI_XIANG_MU);
        }
        return projectById;
    }
}
